package com.syqc.videos

import android.view.View
import androidx.core.view.isVisible
import autodispose2.autoDispose
import com.syqc.comlib.base.BaseActivity
import com.syqc.monitor.databinding.ActivityPlatPlayerBinding
import com.syqc.net.ServiceHelper
import com.syqc.utils.Keys
import com.syqc.utils.VideoHostUtil
import java.util.*

class PlatPlayerActivity :
    BaseActivity<ActivityPlatPlayerBinding>(ActivityPlatPlayerBinding::inflate) {
    private var uuid = ""

    private var videoHost = ""

    override fun initView() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val title = intent.getStringExtra(Keys.INTENT_TITLE)
        val fileName = intent.getStringExtra(Keys.INTENT_DATA)
        val duration = intent.getLongExtra(Keys.INTENT_OTHER, 0)
        val host= VideoHostUtil.getVideoHost(carId!!)
        videoHost = VideoHostUtil.getHost(host)
        uuid = UUID.randomUUID().toString()
        val url = "$videoHost/httpflv.splay.$uuid.$carId.$fileName"

        binding.platPlayer.setUp(url, false, title)
        binding.platPlayer.duration = duration
        binding.platPlayer.titleTextView.isVisible = true
        binding.platPlayer.backButton.isVisible = true

    }


    override fun setListener() {
        binding.platPlayer.backButton.setOnClickListener {
            finish()
        }

        binding.platPlayer.startButton.setOnClickListener {
            if (binding.platPlayer.isInPlayingState) {

            } else {
                binding.platPlayer.startPlayLogic()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        binding.platPlayer.onVideoResume()
    }

    override fun onPause() {
        super.onPause()
        binding.platPlayer.onVideoPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        closePlayer()
        binding.root.postDelayed({
            binding.platPlayer.release()
        }, 100)

    }

    ////////////////////////////////////////////////////////////////////////////////////////////
    private fun closePlayer() {
        val url = "$videoHost/httpflv.close.$uuid"
        ServiceHelper.getRepository().getVideoState(url)
            .autoDispose(scopeProvider).subscribe({

            }, {

            })
    }


    ////////////////////////////////////////////////////////////////////////////////////////////
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }
}